package com.glory.brepda.activity.lanjian;


import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.activity.mfg.AssemblymaterialinfoActivity;
import com.glory.brepda.adapter.LotBatchListAdapter;
import com.glory.brepda.adapter.LotSubmissionhisAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.EqpBody;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotSubmissionEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.OnClick;

public class BreLotSubmissonhisActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpid)
    FreeUI_ClearEditText etEqpid;
    @BindView(R.id.etcomment)
    FreeUI_ClearEditText etcomment;
    @BindView(R.id.flMark)
    FrameLayout flMark;
    @BindView(R.id.ivMark)
    ImageView ivMark;
    @BindView(R.id.tvMark)
    TextView tvMark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    private LotSubmissionhisAdapter mAdapter;
    private FreeUI_GeneralPop markPop;
    private ArrayList<String> marklist = new ArrayList<>(Arrays.asList("A班","B班","C班"));

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lotsubmission;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setVisibility(View.GONE);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvLotList, this);
        mAdapter = new LotSubmissionhisAdapter(null);
        mAdapter.openLoadAnimation(LotSubmissionhisAdapter.ALPHAIN);
        mAdapter.isFirstOnly(false);
        rvLotList.setAdapter(mAdapter);

        flMark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (markPop != null) {
                    if (markPop.isShowing()) {
                        markPop.dismiss();
                    }
                } else {
                    markPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flMark.getWidth(),marklist , ivMark, (ty, i) -> {
                        tvMark.setText(ty);
                        Toast.makeText(getApplicationContext(),ty,Toast.LENGTH_SHORT).show();
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivMark);
                markPop.showAsDropDown(flMark, 0, 0);
            }
        });

        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(BreLotSubmissonhisActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    submit();
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });

        initEdit(etEqpid, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<EqpEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpEntity>>
                    (new TypeToken<BaseResponseBean<EqpEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<EqpEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(BreLotSubmissonhisActivity.this, true, getSupportFragmentManager())) {
                    }

                }

                @Override
                public void onError(Response<BaseResponseBean<EqpEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "MLOTKITTING");

                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "EQPBYID");
            EqpBody eqpBody = new EqpBody("",etEqpid.getText().toString(),"");
            map.add("EQUIPMENT",new JsonParser().parse(new Gson().toJson(eqpBody)).getAsJsonObject());
            NetUtils.requestPostNet("IF.BREQUERYEQP", "Common", BreLotSubmissonhisActivity.this, map, callBack);
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }

    private void submit(){

        if(getText(etLot).equals("")){
            showToast("请先扫描批次信息", false);
            return;
        }
        if(getText(etEqpid).equals("")){
            showToast("请先扫描设备编号", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<LotSubmissionEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotSubmissionEntity>>
                (new TypeToken<BaseResponseBean<LotSubmissionEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotSubmissionEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if(response.body().getResponse().getBody().getResult().getreturnTime() == null ||
                            response.body().getResponse().getBody().getResult().getreturnTime().equals("")){
                        showToast("保存成功", true);
                        mAdapter.addData(0, response.body().getResponse().getBody().getResult());
                    }else{
                        showToast("归批成功", true);
                        mAdapter.addData(0, response.body().getResponse().getBody().getResult());
                    }
                    clean();
                    etLot.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotSubmissionEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDA_WIP_LOT_SUBMISSIONHIS");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOT_ID", getText(etLot));
        map.addProperty("EQP_ID", getText(etEqpid));
        map.addProperty("GROUP1", tvMark.getText().toString());
        map.addProperty("COMMENT1", etcomment.getText().toString());
        NetUtils.requestPostNet("IF.PDA_WIP_LOT_SUBMISSIONHIS", "Common", BreLotSubmissonhisActivity.this, map, callBack);
    }

    public void clean(){
        etcomment.setText("");
        etLot.setText("");
    }

    @Override
    protected void initData() {

    }

}